<?php
session_start ();
require_once ("../../commun/ouvre_base.php");
// ~ $link = mysqli_connect($host, $userbase, $passwd) or die('Impossible de se connecter pour récupérer niveau : ' . mysql_error());
mysql_select_db ( $db ) or die ( 'Impossible de s&#233;lectionner la base de donn&#233;es' );
// ~ mysqli_select_db($link,$db) or die('Impossible de s&#233;lectionner la base de donn&#233;es');



$_SESSION ['pathway'] = '../..';
$_SESSION ['pathway_js'] = '/genetiquespataxeurospa';
include_once ($_SESSION ['pathway'] . "/header.php");

if (isset ( $_GET ['action'] )) {
	$action = $_GET ['action'];
}
if (isset ( $_GET ['index'] )) {
	$index = $_GET ['index'];
}

// ~ print_r($_POST);

foreach ( $_POST as $key => $value ) {
	if ($key != '') {
		$value = mysql_real_escape_string ( $value );
		// ~ $value = mysqli_real_escape_string($link,$value);
		$value = str_replace ( ',', '.', $value );
		$_POST [$key] = $value;
	}
}

$allfichiers = '';
if (isset ( $_POST ['code1'] )) {
	$code1 = strtoupper ( $_POST ['code1'] );
} else {
	$code1 = '';
}
if (isset ( $_POST ['code2'] )) {
	$code2 = strtoupper ( $_POST ['code2'] );
} else {
	$code2 = '';
}
if (isset ( $_POST ['code3'] )) {
	$code3 = strtoupper ( $_POST ['code3'] );
} else {
	$code3 = '';
}
if (isset ( $_POST ['code4'] )) {
	$code4 = strtoupper ( $_POST ['code4'] );
} else {
	$code4 = '';
}
if (isset ( $_POST ['code5'] )) {
	$code5 = strtoupper ( $_POST ['code5'] );
} else {
	$code5 = '';
}
if (isset ( $_POST ['code5'] )) {
	$code5 = $_POST ['code5'];
	$table_individus_screening = explode ( ';', $code5 );
} else {
	$code5 = '';
}
if (isset ( $_POST ['codeexterne'] )) {
	$codeexterne = $_POST ['codeexterne'];
	$table_individus_codeexterne = explode ( ';', $codeexterne );
} else {
	$codeexterne = '';
}
if (isset ( $_POST ['codegenethon'] )) {
	$codegenethon = $_POST ['codegenethon'];
} else {
	$codegenethon = '';
	$table_individus_codegenethon = explode ( ';', $codegenethon );
}
if (isset ( $_POST ['centre'] )) {
	$centre = $_POST ['centre'];
} else {
	$centre = '';
	$centre = strtoupper ( $centre );
}
if (isset ( $_POST ['neurologist'] )) {
	$neurologist = $_POST ['neurologist'];
} else {
	$neurologist = '';
	$neurologist = strtoupper ( $neurologist );
}
if (isset ( $_POST ['gene'] )) {
	$gene = $_POST ['gene'];
} else {
	$gene = '';
}
if (isset ( $_POST ['alias'] )) {
	$alias = $_POST ['alias'];
} else {
	$alias = '';
}
if (isset ( $_POST ['transcript'] )) {
	$transcript = $_POST ['transcript'];
} else {
	$transcript = '';
	$transcript = mysql_real_escape_string ( $transcript );
}
// ~ if(isset($_POST['transcript'])){$transcript = $_POST['transcript'];}else{$transcript = '';$transcript = mysqli_real_escape_string($link,$transcript);}
if (isset ( $_POST ['statut'] )) {
	$statut = $_POST ['statut'];
} else {
	$statut = '';
}
if (isset ( $_POST ['techniquescreening'] )) {
	$techniquescreening = $_POST ['techniquescreening'];
} else {
	$techniquescreening = '';
}
if (isset ( $_POST ['operator'] )) {
	$operator = $_POST ['operator'];
} else {
	$operator = '';
}
if (isset ( $_POST ['manip_nr'] )) {
	$manip_nr = $_POST ['manip_nr'];
} else {
	$manip_nr = '';
}
if (isset ( $_POST ['manip_date'] )) {
	$manip_date = $_POST ['manip_date'];
} else {
	$manip_date = '';
}
if (isset ( $_POST ['platform'] )) {
	$platform = $_POST ['platform'];
} else {
	$platform = '';
}
if (isset ( $_POST ['commentairessequencage'] )) {
	$commentairessequencage = $_POST ['commentairessequencage'];
} else {
	$commentairessequencage = '';
	$commentairessequencage = mysql_real_escape_string ( $commentairessequencage );
}
// ~ if(isset($_POST['commentairessequencage'])){ $commentairessequencage = $_POST['commentairessequencage']; }else{ $commentairessequencage = ''; $commentairessequencage = mysqli_real_escape_string($link,$commentairessequencage);}
if (isset ( $_POST ['commentairespuceshautedensite'] )) {
	$commentairespuceshautedensite = $_POST ['commentairespuceshautedensite'];
} else {
	$commentairespuceshautedensite = '';
	$commentairespuceshautedensite = mysql_real_escape_string ( $commentairespuceshautedensite );
}
// ~ if(isset($_POST['commentairespuceshautedensite'])){ $commentairespuceshautedensite = $_POST['commentairespuceshautedensite']; }else{ $commentairespuceshautedensite = ''; $commentairespuceshautedensite = mysqli_real_escape_string($link,$commentairespuceshautedensite); }
if (isset ( $_POST ['commentairepolymorphismes'] )) {
	$commentairepolymorphismes = $_POST ['commentairepolymorphismes'];
} else {
	$commentairepolymorphismes = '';
}
if (isset ( $_POST ['modescreening'] )) {
	$modescreening = $_POST ['modescreening'];
} else {
	$modescreening = '';
}
if (isset ( $_POST ['zygosity'] )) {
	$zygosity = $_POST ['zygosity'];
} else {
	$zygosity = '';
}
if (isset ( $_POST ['mutation1'] )) {
	$mutation1 = $_POST ['mutation1'];
} else {
	$mutation1 = '';
	$mutation1 = mysql_real_escape_string ( $mutation1 );
}
// ~ if(isset($_POST['mutation1'])){ $mutation1 = $_POST['mutation1']; }else{ $mutation1 = ''; $mutation1 = mysqli_real_escape_string($link,$mutation1);}
if (isset ( $_POST ['mutation2'] )) {
	$mutation2 = $_POST ['mutation2'];
} else {
	$mutation2 = '';
	$mutation2 = mysql_real_escape_string ( $mutation2 );
}
if (isset ( $_POST ['coding_position'] )) {
	$coding_position = $_POST ['coding_position'];
} else {
	$coding_position = '';
	$coding_position = mysql_real_escape_string ( $coding_position );
}
if (isset ( $_POST ['genomic_position'] )) {
	$genomic_position = $_POST ['genomic_position'];
} else {
	$genomic_position = '';
	$genomic_position = mysql_real_escape_string ( $genomic_position );
}
if (isset ( $_POST ['protein_position'] )) {
	$protein_position = $_POST ['protein_position'];
} else {
	$protein_position = '';
	$protein_position = mysql_real_escape_string ( $protein_position );
}
if (isset ( $_POST ['splicing_position'] )) {
	$splicing_position = $_POST ['splicing_position'];
} else {
	$splicing_position = '';
	$splicing_position = mysql_real_escape_string ( $splicing_position );
}
if (isset ( $_POST ['coding_position2'] )) {
	$coding_position2 = $_POST ['coding_position2'];
} else {
	$coding_position2 = '';
	$coding_position2 = mysql_real_escape_string ( $coding_position2 );
}
if (isset ( $_POST ['genomic_position2'] )) {
	$genomic_position2 = $_POST ['genomic_position2'];
} else {
	$genomic_position2 = '';
	$genomic_position2 = mysql_real_escape_string ( $genomic_position2 );
}
if (isset ( $_POST ['protein_position2'] )) {
	$protein_position2 = $_POST ['protein_position2'];
} else {
	$protein_position2 = '';
	$protein_position2 = mysql_real_escape_string ( $protein_position2 );
}
if (isset ( $_POST ['splicing_position2'] )) {
	$splicing_position2 = $_POST ['splicing_position2'];
} else {
	$splicing_position2 = '';
	$splicing_position2 = mysql_real_escape_string ( $splicing_position2 );
}

// ~ if(isset($_POST['mutation2'])){ $mutation2 = $_POST['mutation2']; }else{ $mutation2 = ''; $mutation2 = mysqli_real_escape_string($link,$mutation2);}
if (isset ( $_POST ['allele1'] )) {
	$allele1 = $_POST ['allele1'];
} else {
	$allele1 = '';
	$allele1 = mysql_real_escape_string ( $allele1 );
}
// ~ if(isset($_POST['allele1'])){ $allele1 = $_POST['allele1']; }else{ $allele1 = ''; $allele1 = mysqli_real_escape_string($link,$allele1);}
if (isset ( $_POST ['allele2'] )) {
	$allele2 = $_POST ['allele2'];
} else {
	$allele2 = '';
	$allele2 = mysql_real_escape_string ( $allele2 );
}
// ~ if(isset($_POST['allele2'])){ $allele2 = $_POST['allele2']; }else{ $allele2 = ''; $allele2 = mysqli_real_escape_string($link,$allele2);}
if (isset ( $_POST ['cosegregation'] )) {
	$cosegregation = $_POST ['cosegregation'];
} else {
	$cosegregation = '';
}
if (isset ( $_POST ['commentaireverification'] )) {
	$commentaireverification = $_POST ['commentaireverification'];
} else {
	$commentaireverification = '';
	$commentaireverification = mysql_real_escape_string ( $commentaireverification );
}
// ~ if(isset($_POST['commentaireverification'])){ $commentaireverification = $_POST['commentaireverification']; }else{ $commentaireverification = ''; $commentaireverification = mysqli_real_escape_string($link,$commentaireverification);}
if (isset ( $_POST ['commentairesscreening'] )) {
	$commentairesscreening = $_POST ['commentairesscreening'];
} else {
	$commentairesscreening = '';
	$commentairesscreening = mysql_real_escape_string ( $commentairesscreening );
}
// ~ if(isset($_POST['commentairesscreening'])){ $commentairesscreening = $_POST['commentairesscreening']; }else{ $commentairesscreening = ''; $commentairesscreening = mysqli_real_escape_string($link,$commentairesscreening);}
if (isset ( $_POST ['date'] )) {
	$date = $_POST ['date'];
} else {
	$date = '';
}
if (isset ( $_POST ['user'] )) {
	$user = strtoupper ( $_POST ['user'] );
} else {
	$user = '';
}
$last_modifier = strtoupper ( $_SESSION ['id'] ); // dès qu'on soumet le formulaire, le last_modifier est actualisé;}
if (isset ( $_POST ['polymorphismes'] )) {
	$polymorphismes = $_POST ['polymorphismes'];
} else {
	$polymorphismes = '';
	$polymorphismes = mysql_real_escape_string ( $polymorphismes );
}
// ~ if(isset($_POST['polymorphismes'])){ $polymorphismes = $_POST['polymorphismes']; }else{ $polymorphismes = ''; $polymorphismes = mysqli_real_escape_string($link,$polymorphismes);}
if (isset ( $_POST ['diagnosticscreening'] )) {
	$diagnosticscreening = $_POST ['diagnosticscreening'];
} else {
	$diagnosticscreening = '';
}
if (isset ( $_POST ['validation'] )) {
	$validation = $_POST ['validation'];
} else {
	$validation = '';
}
if (isset ( $_POST ['verifvalidationscreening'] )) {
	$verifvalidationscreening = $_POST ['verifvalidationscreening'];
} else {
	$verifvalidationscreening = '';
}
if (isset ( $_POST ['afaire'] )) {
	$afaire = $_POST ['afaire'];
	$afaire = mysql_real_escape_string ( $afaire );
} else {
	$afaire = '';
}
// ~ if(isset($_POST['afaire'])){ $afaire = $_POST['afaire']; $afaire = mysqli_real_escape_string($link,$afaire);}else{ $afaire = ''; }
if (isset ( $_POST ['commentairesconclusion'] )) {
	$commentairesconclusion = $_POST ['commentairesconclusion'];
} else {
	$commentairesconclusion = '';
	$commentairesconclusion = mysql_real_escape_string ( $commentairesconclusion );
}
// ~ if(isset($_POST['commentairesconclusion'])){ $commentairesconclusion = $_POST['commentairesconclusion']; }else{ $commentairesconclusion = ''; $commentairesconclusion = mysqli_real_escape_string($link,$commentairesconclusion);}
if (isset ( $_POST ['fichiersattaches'] ) and $_POST ['fichiersattaches'] != '') {
	$fichiersattaches = $_POST ['fichiersattaches'];
} else {
	$fichiersattaches = '';
}

$allfichiers .= $fichiersattaches;

// ~ echo "fichieraajouter ".$_FILES['fichieraajouter']['name']."</br>";
// ~ echo "fichiersattaches $fichiersattaches</br>";

if (isset ( $_FILES ['fichieraajouter'] ) and $_FILES ['fichieraajouter'] ['error'] == 0) {
	$tabledate = getdate ();
	$id_unique = $tabledate ['seconds'] . $tabledate ['minutes'] . $tabledate ['hours'] . $tabledate ['mday'] . $tabledate ['mon'] . $tabledate ['year'];
	
	move_uploaded_file ( $_FILES ['fichieraajouter'] ['tmp_name'], '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) );
	chmod ( '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ), 0777 );
	$allfichiers .= $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) . ";";
}
// ~ echo "allfichiers $allfichiers</br>";

if (isset ( $_GET ['action'] ) and $_GET ['action'] == 'edit') {
	$update = "UPDATE `$db`.`screening` SET 
	`code1` = '$code1',
	`code2` = '$code2',
	`code3` = '$code3',
	`code4` = '$code4',
	`code5` = '$code5',
	`codeexterne` = '$codeexterne',
	`codegenethon` = '$codegenethon',
	`centre` = '$centre',
	`neurologist` = '$neurologist',
	`gene` = '$gene',
	`alias` = '$alias',
	`transcript` = '$transcript',
	`statut` = '$statut',
	`techniquescreening` = '$techniquescreening',
	`operator` = '$operator',
	`manip_nr` = '$manip_nr',
	`manip_date` = '$manip_date',
	`platform` = '$platform',
	`commentairessequencage` = '$commentairessequencage',
	`commentairespuceshautedensite` = '$commentairespuceshautedensite',
	`commentairepolymorphismes` = '$commentairepolymorphismes',
	`modescreening` = '$modescreening',
	`zygosity` = '$zygosity',
	`mutation1` = '$mutation1',
	`mutation2` = '$mutation2',
	`coding_position` = '$coding_position',
	`genomic_position` = '$genomic_position',
	`protein_position` = '$protein_position',
	`splicing_position` = '$splicing_position',
	`coding_position2` = '$coding_position2',
	`genomic_position2` = '$genomic_position2',
	`protein_position2` = '$protein_position2',
	`splicing_position2` = '$splicing_position2',
	`allele2` = '$allele2',
	`cosegregation` = '$cosegregation',
	`commentaireverification` = '$commentaireverification',
	`commentairesscreening` = '$commentairesscreening',
	`fichiersattaches` = '$allfichiers',
	`date` = '$date',
	`user` = '$user',
	`last_modifier` = '$last_modifier',
	`polymorphismes` = '$polymorphismes',
	`commentairepolymorphismes` = '$commentairepolymorphismes',
	`diagnosticscreening` = '$diagnosticscreening',
	`validation` = '$validation',
	`verifvalidationscreening` = '$verifvalidationscreening',
	`afaire` = '$afaire',
	`commentairesconclusion` = '$commentairesconclusion'
	WHERE `screening`.`index` = '$index';";
	// ~ echo $update;
	mysql_query ( $update ) or die ( "Can't modify entry : " . mysql_error () );
	// ~ mysqli_query($link,$update) or die("Can't modify entry : " . mysql_error());
} else if (isset ( $_GET ['action'] ) and $_GET ['action'] == 'insert') {
	$nombre_individus = count ( $table_individus_screening );
	
	for($i = 0; $i < $nombre_individus; $i ++) {
		
		$recupereindexmaxscreening = "select MAX(`index`) FROM `screening`";
		$resultindex = mysql_query ( $recupereindexmaxscreening ) or die ( "Impossible de r&eacute;cup&eacute;rer l'index de la table screening" );
		// ~ $resultindex = mysqli_query($link,$recupereindexmaxscreening) or die ("Impossible de r&eacute;cup&eacute;rer l'index de la table screening");
		while ( $ligne = mysql_fetch_row ( $resultindex ) ) {
			// ~ while ($ligne = mysqli_fetch_row($resultindex)) {
			$indexscreening = $ligne [0] + 1;
		}
		
		$code5 = $table_individus_screening [$i];
		if (isset ( $table_individus_codeexterne [$i] )) {
			$codeexterne = $table_individus_codeexterne [$i];
		}
		$insertscreening = "INSERT INTO `$db`.`screening` (
		`index`,
		`code1`,
		`code2`,
		`code3`,
		`code4`,
		`code5`,
		`codeexterne`,
		`codegenethon`,
		`centre`,
		`neurologist`,
		`gene`,
		`alias`,
		`transcript`,
		`statut`,
		`techniquescreening`,
		`commentairessequencage`,
		`commentairespuceshautedensite`,
		`commentairepolymorphismes`,
		`modescreening`,
		`zygosity`,
		`mutation1`,
		`mutation2`,
		`coding_position`,
		`genomic_position`,
		`protein_position`,
		`splicing_position`,
		`coding_position2`,
		`genomic_position2`,
		`protein_position2`,
		`splicing_position2`,
		`allele1`,
		`allele2`,
		`cosegregation`,
		`commentaireverification`,
		`commentairesscreening`,
		`fichiersattaches`,
		`date`,
		`user`,
		`last_modifier`,
		`polymorphismes`,
		`diagnosticscreening`,
		`validation`,
		`verifvalidationscreening`,
		`afaire`,
		`commentairesconclusion`)
		VALUES(
		'$indexscreening',
		'$code1',
		'$code2',
		'$code3',
		'$code4',
		'$code5',
		'$codeexterne',
		'$codegenethon',
		'$centre',
		'$neurologist',
		'$gene',
		'$alias',
		'$transcript',
		'$statut',
		'$techniquescreening',
		'$commentairessequencage',
		'$commentairespuceshautedensite',
		'$commentairepolymorphismes',
		'$modescreening',
		'$zygosity',
		'$mutation1',
		'$mutation2',
		'$coding_position',
		'$genomic_position',
		'$protein_position',
		'$splicing_position',
		'$coding_position2',
		'$genomic_position2',
		'$protein_position2',
		'$splicing_position2',
		'$allele1',
		'$allele2',
		'$cosegregation',
		'$commentaireverification',
		'$commentairesscreening',
		'$allfichiers',
		'$date',
		'$user',
		'$last_modifier',
		'$polymorphismes',
		'$diagnosticscreening',
		'$validation',
		'$verifvalidationscreening',
		'$afaire',
		'$commentairesconclusion');";
		// ~ echo $insertscreening;
		mysql_query ( mysql_real_escape_string($insertscreening)) or die ( "Impossible d'ins&eacute;rer donn&eacute;es dans table screening : " . mysql_error () );
		// ~ mysqli_query($link,$insertscreening) or die("Impossible d'ins&eacute;rer donn&eacute;es dans table screening : " . mysql_error());
	}
}

if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'upload') {
	$_POST = array ();
	echo "<script>window.location.href=\"../screening.php?action=edit&index=" . $index . "&table=screening\"</script>";
} else if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'Ok') {
	$_POST = array ();
	echo "<script>window.location.href=\"./../../start.php\"</script>";
} else {
	$_POST = array ();
	echo "<script>window.location.href=\"../screening.php?action=edit&index=" . $index . "&table=screening\"</script>";
}

?>
